<?php
/**
 * Created by PhpStorm.
 * User: hjl
 * Date: 2016/9/2
 * Time: 11:37
 */

namespace Admin\Controller;

use Admin\Extend\Page;

class WarnController extends BaseController
{
    public function checkWarn()
    {
        $time = I('post.time');
        $time = intval($time / 1000);

        $condition['warn_at'] = array('gt', $time);
        $result = D("warnings")->where($condition)->find();

        if ($result != null) {
            $data = array(
                'status' => true,
                'warn_id' => $result['id'],
                'warn_obj' => $result['susp_id'],
                'warn_type' => $result['susp_con']
            );
        } else {
            $data = array(
                'status' => false,
                'warn_id' => 12,
                'warn_obj' => $result['susp_id'],
                'warn_type' => $result['susp_con']
            );
        }
        $this->ajaxReturn($data, "json");
    }


    public function toInfo()
    {
        $sid = session('adminId');
        //判断用户是否登陆
        if (!isset($sid)) {
            redirect(U('Login/index'));
            return;
        }
    }

    public function index($start_at = '', $end_at = '')
    {
        if (IS_POST) {
            $start_at = I('post.start_at');
            $end_at = I('post.end_at');
        } elseif (IS_GET) {
            $start_at = I('get.start_at');
            $end_at = I('get.end_at');
        }

        if ($start_at === "" || $end_at == "") {
            $model = D('warnings');
        } else {

            $start = strtotime($start_at);
            $end = strtotime($end_at);
            if ($start != 0 && $end != 0) {
                $condition['warn_at'] = array(array('egt', $start), array('elt', $end));
            }
            $model = D('warnings')->where($condition);
        }

        /* 查询满足要求的总记录数 */
        $count = $model->where($condition)->count();

        /* 实例化分页类 传入总记录数和每页显示的记录数(15) */
        $page = new Page($count, 15);

        /* 分页显示输出 */
        $show = $page->show();
        $post = $model->limit($page->firstRow . ',' . $page->listRows)->where($condition)->field('warnings.cj_uid as cj_uid, warnings.cj_user_name as cj_name, warnings.susp_id as susp_id,warnings.susp_name as susp_name,warnings.id as id,warnings.susp_con as susp_con,warnings.warn_at as warn_at')->order('id DESC')->select();
        $this->assign('model', $post);
        $this->assign('page', $show);
        $this->display();
    }

    public function delete()
    {

        if (!IS_POST) {
            return;
        }

        $id = I('post.id');
        $condition['id'] = $id;
        $result = D('warnings')->where($condition)->delete();

        if ($result != null) {
            $data["status"] = true;
        } else {
            $data["status"] = false;
        }
        $this->ajaxReturn($data, "json");
    }
}



